package com.app.manage.moudles.push.controller;



import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONObject;
import com.app.manage.moudles.push.entity.TPushTask;
import com.app.manage.moudles.push.service.TPushTaskService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.app.manage.moudles.push.entity.TPushTaskApp;
import com.app.manage.moudles.push.service.TPushTaskAppService;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import com.app.manage.utils.Rsp;
import javax.annotation.Resource;
import java.io.File;
import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.app.manage.utils.PageSearch;
import org.springframework.web.multipart.MultipartFile;

/**
 * (TPushTaskApp)表控制层
 *
 * @author makejava
 * @since 2025-06-04 10:51:55
 */
@RestController
@RequestMapping("tPushTaskApp")
@Api(tags = "TPushTaskApp")
public class TPushTaskAppController  {
    /**
     * 服务对象
     */
    @Resource
    private TPushTaskAppService tPushTaskAppService;

    @Resource
    private TPushTaskService tPushTaskService;

    @PostMapping("findJars")
    @ApiOperation("查询应用列表")
    public Rsp findJars(@RequestBody JSONObject  obj){
        List<Integer> appIdList = obj.getBeanList("appIds", Integer.class);
        Integer taskId = obj.getInt("taskId");
        return Rsp.success(tPushTaskAppService.listAppByTask(taskId,appIdList));
    }

    @PostMapping("list")
    @ApiOperation("列表查询")
    public Rsp list(@RequestBody   TPushTaskApp s){
        LambdaQueryWrapper<TPushTaskApp> query = new LambdaQueryWrapper<>();
        query.eq(TPushTaskApp::getTaskId,s.getTaskId());
        List<TPushTaskApp> tPushTaskAppList = tPushTaskAppService.list(query);
        List<Integer> taskIdList = tPushTaskAppList.stream().map(TPushTaskApp::getTaskId).collect(Collectors.toList());
        List<TPushTask> taskList = tPushTaskService.list(new LambdaQueryWrapper<TPushTask>().in(CollectionUtil.isNotEmpty(taskIdList),TPushTask::getId,taskIdList));
        Map<Integer, Date> taskTimeMap = new HashMap<Integer, Date>(){{
            taskList.forEach(o->{
                put(o.getId(),o.getPushTime());
            });
        }} ;
        tPushTaskAppList.forEach(o->{
            if(taskTimeMap.get(o.getTaskId()) == null){
                o.setPushTime("待定！");
            }else {
                o.setPushTime(DateUtil.format(taskTimeMap.get(o.getTaskId()),"yyyy-MM-dd HH:mm:ss"));
            }
        });
        return Rsp.success(tPushTaskAppList);
    }


    @PostMapping("uploadJarFile")
    @ApiOperation("上传jar")
    public Rsp uploadFile(@RequestParam("files") MultipartFile[] files,
                          @RequestParam("basicPath")String path,
                          @RequestParam("taskId")Integer taskId,
                          @RequestParam("appIdList")List<Integer> appIdList )throws Exception{
        if(files.length == 0){
            return Rsp.fail("上传文件不能为空！");
        }
        return Rsp.success(tPushTaskAppService.uploadJarsAndGenTaskApps(path,files,taskId,appIdList));
    }



}

